package com.ruoyi.recycle.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.recycle.domain.IntegralCommodityEvaluate;
import com.ruoyi.recycle.domain.IntegralOrder;
import com.ruoyi.recycle.dto.IntegralOrderDTO;
import com.ruoyi.recycle.service.IIntegralOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * 积分订单Controller
 *
 * @author ruoyi
 * @date 2022-05-22
 */
@RestController
@RequestMapping("/integralOrder")
@Api(tags = "积分订单")
public class IntegralOrderController extends BaseController {
    @Autowired
    private IIntegralOrderService integralOrderService;

    /**
     * 查询积分订单列表
     */
    @RequiresPermissions("recycle:integralOrder:list")
    @GetMapping("/list")
    public TableDataInfo list(IntegralOrder integralOrder) {
        startPage();
        List<IntegralOrder> list = integralOrderService.selectIntegralOrderList(integralOrder);
        return getDataTable(list);
    }

    /**
     * 分页查询回收订单
     */
    @PostMapping("/integralOrderPageQuery")
    @ApiOperation("分页查询回收订单")
    public IPage<IntegralOrder> integralOrderPageQuery(@RequestBody IntegralOrderDTO queryDTO) throws Exception{
        final IntegralOrder integralOrder = new IntegralOrder();
        BeanUtils.copyProperties(queryDTO, integralOrder);
        final LambdaQueryWrapper<IntegralOrder> wrapper = Wrappers.lambdaQuery(integralOrder);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat old = new SimpleDateFormat("yyyy-MM-dd");
        if (queryDTO.getSearchTimeBegin() != null) {
            wrapper.ge(IntegralOrder::getCreatedTime, simpleDateFormat.parse(old.format(queryDTO.getSearchTimeBegin()) + " 00:00:00"));
        }
        if (queryDTO.getSearchTimeEnd() != null) {
            wrapper.le(IntegralOrder::getCreatedTime, simpleDateFormat.parse(old.format(queryDTO.getSearchTimeEnd()) + " 23:59:59"));
        }
        wrapper.orderByDesc(IntegralOrder::getCreatedTime);
        return integralOrderService.page(new Page<IntegralOrder>()
                .setCurrent(queryDTO.getCurrentPage())
                .setSize(queryDTO.getPageSize()), wrapper);
    }

    /**
     * 导出积分订单列表
     */
    @RequiresPermissions("recycle:integralOrder:export")
    @Log(title = "积分订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出积分订单列表")
    public void export(HttpServletResponse response, IntegralOrder integralOrder) {
        List<IntegralOrder> list = integralOrderService.selectIntegralOrderList(integralOrder);
        ExcelUtil<IntegralOrder> util = new ExcelUtil<IntegralOrder>(IntegralOrder.class);
        util.exportExcel(response, list, "积分订单数据");
    }

    /**
     * 获取积分订单详细信息
     */
    @RequiresPermissions("recycle:integralOrder:query")
    @GetMapping(value = "/{id}")
    @ApiOperation("获取积分订单详细信息")
    @ApiImplicitParam(name = "id", value = "id", dataType = "Long")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(integralOrderService.selectIntegralOrderById(id));
    }

    /**
     * 新增积分订单
     */
    @RequiresPermissions("recycle:integralOrder:add")
    @Log(title = "积分订单", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增积分订单")
    public AjaxResult add(@RequestBody IntegralOrder integralOrder) {
        return toAjax(integralOrderService.insertIntegralOrder(integralOrder));
    }

    /**
     * 修改积分订单
     */
    @RequiresPermissions("recycle:integralOrder:edit")
    @Log(title = "积分订单", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改积分订单")
    public AjaxResult edit(@RequestBody IntegralOrder integralOrder) {
        return toAjax(integralOrderService.updateIntegralOrder(integralOrder));
    }

    /**
     * 删除积分订单
     */
    @RequiresPermissions("recycle:integralOrder:remove")
    @Log(title = "积分订单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation("删除积分订单")
    @ApiImplicitParam(name = "ids", value = "id集合", dataType = "List")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(integralOrderService.deleteIntegralOrderByIds(ids));
    }

    @ApiOperation("后台管理同意申请退款")
    @PostMapping("/applicationForDrawback")
    public AjaxResult applicationForDrawback(@RequestParam(value = "id",required = true) Long id) {

        return integralOrderService.applicationForDrawback(id);
    }
}
